package edu.scu.declaresys.web.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.INewsService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.News;
import edu.scu.declaresys.web.ABaseAction;

/**
 * 
 * @author: zt
 * @since : 2013-7-8
 */
public class NewsAction extends ABaseAction implements ModelDriven<News>
{
	private static final long	serialVersionUID	= 4314483703359914215L;
	
	private News news = new News();
	
	private Integer page;
	
	private INewsService newsService = ServiceFactory.createNewsService();
	
	//
	// action.
	//
	public String newsDetail() throws Exception
	{
		News chainNews = (News)request.getAttribute(IAttrName.NEWS_VO);
		//
		Integer id = (news.getId() == null ? null : news.getId());
		if(id == null && chainNews == null)
		{
			throw new Exception("未指定新闻");
		}
		id = (id == null ? chainNews.getId() : id);
		News vo = newsService.findNewsById(id);
		request.setAttribute(IAttrName.NEWS_VO, vo);
		//
		return Action.SUCCESS;
	}
	
	public String newsList() throws Exception
	{
		if(news.getNewsClassId() == null)
		{
			throw new Exception("未指定新闻类别");
		}
		if(page == null)
		{
			page = 1;
		}
		PageList<News> list = null;
		request.setAttribute(IAttrName.NEWS_LIST, list = newsService.findNewsList(news.getNewsClassId(), page));
		request.setAttribute(IAttrName.NEWS_LIST_SIZE, list.getDataAmt());
		return Action.SUCCESS;
	}
	
	public String newsManage() throws Exception
	{
		if(page == null)
		{
			page = 1;
		}
		PageList<News> list = null;
		request.setAttribute(IAttrName.NEWS_LIST, list = newsService.findNewsList(news.getNewsClassId(), page));
		request.setAttribute(IAttrName.NEWS_LIST_SIZE, list.getDataAmt());
		return Action.SUCCESS;
	}
	
	//
	// write methods.
	//
	public String newsShowAdd() throws Exception
	{
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createNewsClassService().findClassList());
		return Action.SUCCESS;
	}
	
	public String newsAdd() throws Exception
	{
		if(news.getNewsClassId() == null 
			|| news.getTitle() == null
			|| news.getContent() == null)
		{
			throw new Exception("新闻类别、标题、内容不能为空");
		}
		if(newsService.findByTitle(news.getTitle()) != null)
		{
			throw new Exception("新闻标题重复！");
		}
		request.setAttribute(IAttrName.NEWS_VO, newsService.addNews(news));			
		return Action.SUCCESS;	
	}
	
	public String newsDelete() throws Exception
	{
		if(news.getId() == null)
		{
			throw new Exception("未指定要删除的新闻!");
		}
		if(newsService.findNewsById(news.getId()) == null)
		{
			throw new Exception("指定的新闻不存在");
		}
		// Do delete.
		newsService.delete(news.getId());
		return Action.SUCCESS;
	}
	//
	//
	//
	@Override
	public News getModel()
	{
		return news;
	}

	public News getNews()
	{
		return news;
	}

	public void setNews(News news)
	{
		this.news = news;
	}

	public Integer getPage()
	{
		return page;
	}

	public void setPage(Integer page)
	{
		this.page = page;
	}
}
